package base.utils;

import android.animation.TypeEvaluator;
import android.graphics.PointF;

/**
 * Created by Administrator_LZH on 2016/9/19.
 */
public class BezierEvaluator implements TypeEvaluator {

    private float width;
    private float height;
    private PointF pointF1;
    private PointF pointF2;

    public BezierEvaluator(float width, float height) {
        this.width = width;
        this.height = height;
    }

    public BezierEvaluator(PointF pointF1, PointF pointF2) {
        this.width = -1f;
        this.height = -1f;
        this.pointF1 = pointF1;
        this.pointF2 = pointF2;
    }

    @Override
    public Object evaluate(float fraction, Object startValue, Object endValue) {
        final float t = fraction;
        float oneMinusT = 1.0f - t;
        PointF point = new PointF();
        PointF point0 = (PointF) startValue;
        PointF point1;
        PointF point2;
        PointF point3 = (PointF) endValue;
        if (width != -1f) {
            point1 = new PointF();
            point1.set(width, height);
            point2 = new PointF();
            point2.set(point0.x + (point3.x - point0.x) * 2 / 3, point0.y);
        } else {
            point1 = pointF1;
            point2 = pointF2;
        }

        point.x = oneMinusT * oneMinusT * oneMinusT * (point0.x)
                + 3 * oneMinusT * oneMinusT * t * (point1.x)
                + 3 * oneMinusT * t * t * (point2.x)
                + t * t * t * (point3.x);

        point.y = oneMinusT * oneMinusT * oneMinusT * (point0.y)
                + 3 * oneMinusT * oneMinusT * t * (point1.y)
                + 3 * oneMinusT * t * t * (point2.y)
                + t * t * t * (point3.y);
        return point;
    }
}
